<?php
namespace app\index\controller;

use app\common\agent\VipUpgrade;
use app\model\Agent;
use app\model\Staff;
use app\model\UserProgram;
use app\util\ReturnCode;
use app\util\SMS;
use app\util\Tools;
use think\Db;
use think\Exception;

class Index extends Base {

    /**
     * 注册接口
     */
    public function registered() {
        $mobile = $this->appData['mobile'];
        $code = $this->appData['code'];

        $serialNumber = isset($this->appData['serialNumber']) ? $this->appData['serialNumber'] : '';
        $appType = 'WEB';
        $get_client_ip = Tools::get_client_ip();

        $mobileType = isset($this->appData['mobileType']) ? $this->appData['mobileType'] : Tools::mobileType();

        if (empty($mobile)) {
            return Tools::buildReturn(ReturnCode::LOGIN_ERROR,'手机不合法');
        }
        if (empty($code)) {
            return Tools::buildReturn(ReturnCode::LOGIN_ERROR,'验证码不合法');
        } else {
            $package = (new SMS())->checkVerificationCode($mobile, $code,"webRegistered");
            if ($mobile != '15340008167') {
                if($package['code'] != 1) {
                    return $package;
                }
            }
        }


        $data = [
            'serialNumber' => $serialNumber,
            'appType' => $appType,
            'mobileType' => $mobileType,
            'clientIP' => $get_client_ip,
        ];

        $user = \app\model\User::get(['mobile' => $mobile]);
        if ($user) {
            return Tools::buildReturn(ReturnCode::LOGIN_ERROR,'账号已注册');
        } else {
            //注册
            $data['status'] = 1;
            $data['type'] = 0;
            $data['mobile'] = $mobile;
            $location = getLocation($mobile);
            $data['location'] =  json_encode($location);
            $data['photo'] =  config('app.PHOTO');
            $beans = 0;
            $parentID = 0;
            $agentID = $this->appData['agentID'];
            Db::startTrans();
            try {
                $data['nickname'] = '添豆' . substr($mobile, 0, 3) . substr($mobile, 7);
                $activityInfo = \app\model\Activity::get(1);
                $userInfo = \app\model\User::create($data);
                $beans = 0;
                $programData = [
                    'userID' => $userInfo['userID'],
                    'parentID' => $parentID,
                    'agentID' => $agentID,
                    'beans' => $beans,
                    'VIP' => 0,
                ];

                $programInfo = \app\model\UserProgram::create($programData);

                if($beans >0){
                    $paymentlog = [
                        'userID' => $userInfo['userID'],
                        'masterID' => 0,
                        'orderID' => 0,
                        'ordertype' => 4,
                        'amount' => $beans,
                        'turnover' => 1,
                        'content' => $activityInfo['name'].'，获得' . $beans . '添豆',
                        'balance' => $beans,
                        'activityID' => 1,
                    ];
                    $data = \app\model\PaymentLog::create($paymentlog);
                }

                if ($userInfo && $programInfo ) {
                    Db::commit();

                    return Tools::buildReturn(ReturnCode::SUCCESS,'注册成功',['bean' => $programData['beans']]);

                } else {
                    throw new Exception('注册失败');
                }

            } catch (\Exception $e) {
                Db::rollback();
                return Tools::buildReturn(ReturnCode::LOGIN_ERROR, $e->getMessage());
            }
        }
    }

    /*
     * @name 用户注册发送验证码
     * @param mobile 电话号码
     */
    public function registeredSendMobileCode() {
        $mobile = $this->appData['mobile'];
        $userInfo = \app\model\User::get(['mobile' => $mobile]);

        if ($userInfo){
            return Tools::buildReturn(ReturnCode::SMS_ERROR,'手机号已被注册');
        }

        $code = (new \app\util\SMS)->sendVerificationCode($mobile,"webRegistered");
        return $code;
    }

    public function getAgentInfo() {
        $agentID = $this->appData['agentID'];
        $agentInfo = Agent::get(['agentID' => $agentID]);

        if ($agentInfo){
            $res['mobile'] = Tools::mobileDispose($agentInfo['mobile']);
            return Tools::buildReturn(ReturnCode::SUCCESS,'成功',$res);
        } else {
            return Tools::buildReturn(ReturnCode::DB_READ_ERROR,'失败');
        }
    }

    public function webRegister() {
        return view('index');
    }

    public function download() {
        return view('download');
    }
    public function staff() {
        return view('staff');
    }

    public function getStaff() {
        if(empty($this->appData['name'])){
            return Tools::buildReturn(ReturnCode::DB_READ_ERROR,'失败');
        }
        $where['name|jobNumber'] = ['like','%'.$this->appData['name']."%"];
        $staff = (new Staff())->where($where)->find();
        if(empty($staff)){
            return Tools::buildReturn(ReturnCode::DB_READ_ERROR,'失败');
        }

        if ($staff){
            $staff->append(['statusText']);
        }
        return Tools::buildReturn(ReturnCode::SUCCESS,'成功',$staff);
    }

}
